package com.forum.community.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.forum.article.vo.param.ArticleParam;
import com.forum.common.utils.R;
import com.forum.community.client.ArticleClient;
import com.forum.community.entity.ForumCommunityArticle;
import com.forum.community.service.ForumCommunityArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author bncgnc
 * @since 2022-01-04
 */
@RestController
@RequestMapping("/community/forumCommunityArticle")
public class ForumCommunityArticleController {

    private ArticleClient articleClient;

    @Autowired
    private ForumCommunityArticleService communityArticleService;


    //新增社区文章,文章模块写文章信息，社区模块加入社区文章表信息
    @PostMapping("addArticle")
    public R addArticle(@RequestBody ForumCommunityArticle communityArticle,@RequestBody ArticleParam articleParam){
        //文章模块添加文章
        articleClient.addCommunityArticle(articleParam);

        boolean i = communityArticleService.save(communityArticle);
        if(i){
            return R.ok("添加社区文章成功！");

        }return R.error("添加社区文章失败！");
    }


    //删除社区文章，删除文章模块的该文章，删除社区文章表的文章
    @DeleteMapping("deleteArticle")
    public R deleteArticle(@RequestBody ForumCommunityArticle communityArticle){

        //文章模块删除文章

        QueryWrapper<ForumCommunityArticle> query = new QueryWrapper<>();
        query.eq("article_id",communityArticle.getArticleId()).eq("community_id",communityArticle.getCommunityId());
        boolean i = communityArticleService.remove(query);
        if(i){
            return R.ok("删除社区文章成功！");

        }return R.error("删除社区文章失败！");
    }

    //根据社区id列出所有社区文章
    @GetMapping("listCommunityArticle/{communityId}")
    public R listCommunityArticle(@PathVariable Long communityId){
        QueryWrapper<ForumCommunityArticle> query = new QueryWrapper<>();
        query.eq("community_id",communityId);
        List<ForumCommunityArticle> communityArticle = communityArticleService.list(query);


        //远程调用获取所有社区文章


        return R.ok().put("communityArticle",communityArticle);
    }


}

